const { merge } = require('webpack-merge')
const common = require('./webpack.common')
const path = require('path')
const TerserPlugin = require('terser-webpack-plugin');
const CssMinimizerWebpackPlugin = require('css-minimizer-webpack-plugin');

module.exports = merge(common, {
    optimization: {
        minimize: true,
        minimizer: [
            new TerserPlugin({
                parallel: true,
                terserOptions: {
                    mangle: true,
                    compress: {
                        drop_console: true,
                        pure_funcs: ['console.infp']
                    },
                    output: {
                        comments: true,
                        beautify: true, // 美化输出
                    }
                }
            }),
            new CssMinimizerWebpackPlugin({
                parallel: true,
                minimizerOptions: {
                    preset: [
                        'default',
                        {
                            discardComments: { removeAll: true },
                            colormin: true
                        }
                    ]
                }
            })
        ],
        splitChunks: {
            chunks: 'all',
            maxAsyncRequests: 2,
            maxInitialRequests: 2,
            // maxSize: 50000,
            cacheGroups: {
                // 提取业务代码中被多个入口复用的公共部分
                commons: {
                    filename: 'common',
                    minChunks: 2, // 👈 被引用2次以上才抽离
                    chunks: 'all',
                    priority: 10,
                    reuseExistingChunk: true,
                },
                // 提取所有 node_modules 中的依赖
                vendor: {
                    test: /[\\/]node_modules[\\/]/,
                    filename: 'vendors-[name].js',
                    chunks: 'all',
                    enforce: true
                },
            }
        },
    },
    devtool: 'cheap-module-source-map',
    mode: 'production'
})